package life.sweet.community.dto;

import life.sweet.community.exception.CustomErrorCode;
import life.sweet.community.exception.CustomException;
import lombok.Data;

@Data
public class ResultDTO<T> {
    private Integer code;
    private String message;
    private T data;

    public static ResultDTO errorOf(Integer code , String message) {
        ResultDTO dto = new ResultDTO();
        dto.setCode(code);
        dto.setMessage(message);
        return dto;
    }

    public static ResultDTO errorOf(CustomErrorCode errorCode) {
        ResultDTO dto = new ResultDTO();
        dto.setCode(errorCode.getCode());
        dto.setMessage(errorCode.getMessage());
        return dto;
    }

    public static ResultDTO errorOf(CustomException e) {
        return errorOf(e.getCode(),e.getMessage());
    }

    public static <T> ResultDTO okOf(T t) {
        ResultDTO dto = new ResultDTO();
        dto.setCode(200);
        dto.setMessage("成功");
        dto.setData(t);
        return dto;
    }

    public static ResultDTO okOf() {
        ResultDTO dto = new ResultDTO();
        dto.setCode(200);
        dto.setMessage("成功");
        return dto;
    }
}
